<?php
/**
 * 运营-公司产品利润率
 * (model)
 */
namespace App\Models\Operation;

use App\Models\Basics\SysCountry;
use App\Models\Basics\SysPlat;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductRakutenSku;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
class ProfitSku extends Model
{
    protected $table = 'operation_profit_sku';

    protected $guarded = [];

    public function country()
    {
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    public static $_inventory = 1;

    public static $_other = 1;

    /**
     * 获取成本
     * @param $tpl //模板
     * @param $salePrice //销售价格
     * @param $headCost //头程费用
     * @param $fbaFee
     * @param $commission
     * @param $return
     * @param $vat
     * @return float
     */
    public static function getCost($tpl,$salePrice,$headCost,$fbaFee,$commission,$return,$vat){
        return round(($tpl +
            $headCost +
            $fbaFee +
            $commission * $salePrice / 100 +
            self::$_inventory * $salePrice / 100 +
            self::$_other * $salePrice / 100 +
            $return * $salePrice / 100 +
            $vat * $salePrice / 100),3);
    }

    public static $_status = [
        0 => '一般',
        1 => '严重',
        2 => '正常',
    ];

    public static function getStatus($status,$salePrice,$fbaFee,$commission,$fbaFeeFlag = true,$createAt = null){
        if($fbaFeeFlag){
            if(($salePrice != 0) && ($fbaFee != 0) && ($commission != 0))return 2;
        }else{
            if(($salePrice != 0) && ($commission != 0))return 2;
        }
        if($createAt){
            $time = strtotime('+1 day',strtotime(date('Y-m-d 18:30:00',strtotime($createAt))));
            if($time < time()){
                return 1;
            }
        }
        return $status;
    }

    /**
     * 添加利润率产品 (Amazon)
     * @param ProductAmazonSku $productAmazonSku
     * @param $rateOdds
     * @param $tplRMB
     * @param $weight
     * @param $airFee
     * @param $seaFee
     * @param $vat
     * @param bool $avgFee
     */
    public static function addProfitSku(ProductAmazonSku $productAmazonSku,$rateOdds,$tplRMB,$weight,$airFee,$seaFee,$vat,$avgFee = false){
        $count = self::query()
            ->where('country_id',$productAmazonSku->country_id)
            ->where('psku_id',$productAmazonSku->psku_id)
            ->count();
        $shipWeight = !$avgFee ? $weight : 1;
        if($count == 0){
            $date = date('Y-m-d H:i:s');
            self::query()->insert([[
                    'country_id' => $productAmazonSku->country_id,
                    'psku_id' => $productAmazonSku->psku_id,
                    'transport' => 1,
                    'seller_id' => $productAmazonSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $seaFee / $rateOdds,3),
                    'vat' => $vat,
                    'created_at' => $date,
                    'updated_at' => $date,
                ],
                [
                    'country_id' => $productAmazonSku->country_id,
                    'psku_id' => $productAmazonSku->psku_id,
                    'transport' => 2,
                    'seller_id' => $productAmazonSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $airFee / $rateOdds,3),
                    'vat' => $vat,
                    'created_at' => $date,
                    'updated_at' => $date,
                ]
            ]);
        }else{
            self::query()
                ->where('country_id',$productAmazonSku->country_id)
                ->where('psku_id',$productAmazonSku->psku_id)
                ->where('transport' , 1)
                ->update([
                    'seller_id' => $productAmazonSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $seaFee / $rateOdds,3),
                    'vat' => $vat,
                ]);
            self::query()
                ->where('country_id',$productAmazonSku->country_id)
                ->where('psku_id',$productAmazonSku->psku_id)
                ->where('transport' , 2)
                ->update([
                    'seller_id' => $productAmazonSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $airFee / $rateOdds,3),
                    'vat' => $vat,
                ]);
        }
    }

    /**
     * 添加利润率产品(Rakuten)
     * @param ProductRakutenSku $productRakutenSku
     * @param $rateOdds
     * @param $tplRMB
     * @param $weight
     * @param $airFee
     * @param $seaFee
     * @param $vat
     * @param false $avgFee
     * @param int $platId
     */
    public static function addProfitSkuRakuten(ProductRakutenSku $productRakutenSku,$rateOdds,$tplRMB,$weight,$airFee,$seaFee,$vat,$avgFee = false,$platId = SysPlat::PLAT_RAKUTEN){
        $count = self::query()
            ->where('plat_id', $platId)
            ->where('country_id',$productRakutenSku->country_id)
            ->where('psku_id',$productRakutenSku->psku_id)
            ->count();
        $shipWeight = !$avgFee ? $weight : 1;
        if($count == 0){
            $date = date('Y-m-d H:i:s');
            self::query()->insert([
                [
                    'plat_id' => $platId,
                    'country_id' => $productRakutenSku->country_id,
                    'psku_id' => $productRakutenSku->psku_id,
                    'transport' => 1,
                    'seller_id' => $productRakutenSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $seaFee / $rateOdds,3),
                    'vat' => $vat,
                    'created_at' => $date,
                    'updated_at' => $date,
                ],
                [
                    'plat_id' => $platId,
                    'country_id' => $productRakutenSku->country_id,
                    'psku_id' => $productRakutenSku->psku_id,
                    'transport' => 2,
                    'seller_id' => $productRakutenSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $airFee / $rateOdds,3),
                    'vat' => $vat,
                    'created_at' => $date,
                    'updated_at' => $date,
                ]
            ]);
        }else{
            self::query()
                ->where('plat_id', $platId)
                ->where('country_id',$productRakutenSku->country_id)
                ->where('psku_id',$productRakutenSku->psku_id)
                ->where('transport', 1)
                ->update([
                    'seller_id' => $productRakutenSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $seaFee / $rateOdds,3),
                    'vat' => $vat,
                ]);
            self::query()
                ->where('plat_id', $platId)
                ->where('country_id',$productRakutenSku->country_id)
                ->where('psku_id',$productRakutenSku->psku_id)
                ->where('transport', 2)
                ->update([
                    'seller_id' => $productRakutenSku->seller_id,
                    'rate_odds' => $rateOdds,
                    'tpl_rmb' => $tplRMB,
                    'weight' => $weight,
                    'head_cost' => round($shipWeight * $airFee / $rateOdds,3),
                    'vat' => $vat,
                ]);
        }
    }

}
